<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.models.ClusterGraph &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.models.ClusterGraph</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>

<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">defaultdict</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">pgmpy.base</span> <span class="k">import</span> <span class="n">UndirectedGraph</span>
<span class="kn">from</span> <span class="nn">pgmpy.factors</span> <span class="k">import</span> <span class="n">factor_product</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern.six.moves</span> <span class="k">import</span> <span class="nb">filter</span><span class="p">,</span> <span class="nb">range</span><span class="p">,</span> <span class="nb">zip</span>


<div class="viewcode-block" id="ClusterGraph"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph">[docs]</a><span class="k">class</span> <span class="nc">ClusterGraph</span><span class="p">(</span><span class="n">UndirectedGraph</span><span class="p">):</span>
    <span class="sd">r&quot;&quot;&quot;</span>
<span class="sd">    Base class for representing Cluster Graph.</span>

<span class="sd">    Cluster graph is an undirected graph which is associated with a subset of variables. The graph contains undirected</span>
<span class="sd">    edges that connects clusters whose scopes have a non-empty intersection.</span>

<span class="sd">    Formally, a cluster graph is  :math:`\mathcal{U}` for a set of factors :math:`\Phi` over :math:`\mathcal{X}` is an</span>
<span class="sd">    undirected graph, each of whose nodes :math:`i` is associated with a subset :math:`C_i \subseteq X`. A cluster</span>
<span class="sd">    graph must be family-preserving - each factor :math:`\phi \in \Phi` must be associated with a cluster C, denoted</span>
<span class="sd">    :math:`\alpha(\phi)`, such that :math:`Scope[\phi] \subseteq C_i`. Each edge between a pair of clusters :math:`C_i`</span>
<span class="sd">    and :math:`C_j` is associated with a sepset :math:`S_{i,j} \subseteq C_i \cap C_j`.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    data: input graph</span>
<span class="sd">        Data to initialize graph. If data=None (default) an empty graph is created. The data is an edge list</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    Create an empty ClusterGraph with no nodes and no edges</span>

<span class="sd">    &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">    &gt;&gt;&gt; G = ClusterGraph()</span>

<span class="sd">    G can be grown by adding clique nodes.</span>

<span class="sd">    **Nodes:**</span>

<span class="sd">    Add a tuple (or list or set) of nodes as single clique node.</span>

<span class="sd">    &gt;&gt;&gt; G.add_node((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;))</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)])</span>

<span class="sd">    **Edges:**</span>

<span class="sd">    G can also be grown by adding edges.</span>

<span class="sd">    &gt;&gt;&gt; G.add_edge((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;))</span>

<span class="sd">    or a list of edges</span>

<span class="sd">    &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)),</span>
<span class="sd">    ...                   ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ebunch</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ClusterGraph</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">ebunch</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">ebunch</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">factors</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="ClusterGraph.add_node"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.add_node">[docs]</a>    <span class="k">def</span> <span class="nf">add_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add a single node to the cluster graph.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        node: node</span>
<span class="sd">            A node should be a collection of nodes forming a clique. It can be</span>
<span class="sd">            a list, set or tuple of nodes</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; G = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; G.add_node((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;))</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">set</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Node can only be a list, set or tuple of nodes forming a clique&#39;</span><span class="p">)</span>

        <span class="n">node</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ClusterGraph</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.add_nodes_from"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.add_nodes_from">[docs]</a>    <span class="k">def</span> <span class="nf">add_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add multiple nodes to the cluster graph.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nodes: iterable container</span>
<span class="sd">            A container of nodes (list, dict, set, etc.).</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; G = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.add_edge"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.add_edge">[docs]</a>    <span class="k">def</span> <span class="nf">add_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add an edge between two clique nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v: nodes</span>
<span class="sd">            Nodes can be any list or set or tuple of nodes forming a clique.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; G = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;c&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)),</span>
<span class="sd">        ...                   ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">set_u</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>
        <span class="n">set_v</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">set_u</span><span class="o">.</span><span class="n">isdisjoint</span><span class="p">(</span><span class="n">set_v</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No sepset found between these two edges.&#39;</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">ClusterGraph</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.add_factors"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.add_factors">[docs]</a>    <span class="k">def</span> <span class="nf">add_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">factors</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Associate a factor to the graph.</span>
<span class="sd">        See factors class for the order of potential values</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        *factor: pgmpy.factors.factors object</span>
<span class="sd">            A factor object on any subset of the variables of the model which</span>
<span class="sd">            is to be associated with the model.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        None</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; student.add_node((&#39;Alice&#39;, &#39;Bob&#39;))</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[3, 2],</span>
<span class="sd">        ...                 values=np.random.rand(6))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="n">factors</span><span class="p">:</span>
            <span class="n">factor_scope</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span>
            <span class="n">nodes</span> <span class="o">=</span> <span class="p">[</span><span class="nb">set</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()]</span>
            <span class="k">if</span> <span class="n">factor_scope</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Factors defined on clusters of variable not&#39;</span>
                                 <span class="s1">&#39;present in model&#39;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.get_factors"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.get_factors">[docs]</a>    <span class="k">def</span> <span class="nf">get_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the factors that have been added till now to the graph.</span>

<span class="sd">        If node is not None, it would return the factor corresponding to the</span>
<span class="sd">        given node.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; G = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;c&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)),</span>
<span class="sd">        ...                   ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [2, 2, 2], np.random.rand(8))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;a&#39;, &#39;b&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; phi3 = DiscreteFactor([&#39;a&#39;, &#39;c&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(phi1, phi2, phi3)</span>
<span class="sd">        &gt;&gt;&gt; G.get_factors()</span>
<span class="sd">        &gt;&gt;&gt; G.get_factors(node=(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;))</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">node</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nodes</span> <span class="o">=</span> <span class="p">[</span><span class="nb">set</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()]</span>

            <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">node</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Node not present in Cluster Graph&#39;</span><span class="p">)</span>

            <span class="n">factors</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">set</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span> <span class="o">==</span> <span class="nb">set</span><span class="p">(</span><span class="n">node</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">)</span>
            <span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="n">factors</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.remove_factors"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.remove_factors">[docs]</a>    <span class="k">def</span> <span class="nf">remove_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">factors</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Removes the given factors from the added factors.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[2, 2],</span>
<span class="sd">        ...                 value=np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor)</span>
<span class="sd">        &gt;&gt;&gt; student.remove_factors(factor)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="n">factors</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.get_cardinality"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.get_cardinality">[docs]</a>    <span class="k">def</span> <span class="nf">get_cardinality</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the cardinality of the node</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        node: any hashable python object (optional)</span>
<span class="sd">            The node whose cardinality we want. If node is not specified returns a</span>
<span class="sd">            dictionary with the given variable as keys and their respective cardinality</span>
<span class="sd">            as values.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        int or dict : If node is specified returns the cardinality of the node.</span>
<span class="sd">                      If node is not specified returns a dictionary with the given</span>
<span class="sd">                      variable as keys and their respective cardinality as values.</span>


<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[2, 2],</span>
<span class="sd">        ...                 values=np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; student.add_node((&#39;Alice&#39;, &#39;Bob&#39;))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor)</span>
<span class="sd">        &gt;&gt;&gt; student.get_cardinality()</span>
<span class="sd">        defaultdict(&lt;class &#39;int&#39;&gt;, {&#39;Bob&#39;: 2, &#39;Alice&#39;: 2})</span>

<span class="sd">        &gt;&gt;&gt; student.get_cardinality(node=&#39;Alice&#39;)</span>
<span class="sd">        2</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">node</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">cardinality</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="n">factor</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">node</span> <span class="o">==</span> <span class="n">variable</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">cardinality</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">cardinalities</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">cardinality</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="n">factor</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                    <span class="n">cardinalities</span><span class="p">[</span><span class="n">variable</span><span class="p">]</span> <span class="o">=</span> <span class="n">cardinality</span>
            <span class="k">return</span> <span class="n">cardinalities</span></div>

<div class="viewcode-block" id="ClusterGraph.get_partition_function"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.get_partition_function">[docs]</a>    <span class="k">def</span> <span class="nf">get_partition_function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">r&quot;&quot;&quot;</span>
<span class="sd">        Returns the partition function for a given undirected graph.</span>

<span class="sd">        A partition function is defined as</span>

<span class="sd">        .. math:: \sum_{X}(\prod_{i=1}^{m} \phi_i)</span>

<span class="sd">        where m is the number of factors present in the graph</span>
<span class="sd">        and X are all the random variables present.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import ClusterGraph</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; G = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;), (&#39;a&#39;, &#39;c&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;b&#39;)),</span>
<span class="sd">        ...                   ((&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (&#39;a&#39;, &#39;c&#39;))])</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [2, 2, 2], np.random.rand(8))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;a&#39;, &#39;b&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; phi3 = DiscreteFactor([&#39;a&#39;, &#39;c&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(phi1, phi2, phi3)</span>
<span class="sd">        &gt;&gt;&gt; G.get_partition_function()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_model</span><span class="p">():</span>
            <span class="n">factor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">factor</span> <span class="o">=</span> <span class="n">factor_product</span><span class="p">(</span><span class="n">factor</span><span class="p">,</span> <span class="o">*</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">))])</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">values</span><span class="p">)</span></div>

<div class="viewcode-block" id="ClusterGraph.check_model"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.check_model">[docs]</a>    <span class="k">def</span> <span class="nf">check_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check the model for various errors. This method checks for the following</span>
<span class="sd">        errors.</span>

<span class="sd">        * Checks if factors are defined for all the cliques or not.</span>
<span class="sd">        * Check for running intersection property is not done explicitly over</span>
<span class="sd">          here as it done in the add_edges method.</span>
<span class="sd">        * Checks if cardinality information for all the variables is availble or not. If</span>
<span class="sd">          not it raises an error.</span>
<span class="sd">        * Check if cardinality of random variable remains same across all the</span>
<span class="sd">          factors.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        check: boolean</span>
<span class="sd">            True if all the checks are passed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">clique</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">():</span>
            <span class="n">factors</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">set</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span> <span class="o">==</span> <span class="nb">set</span><span class="p">(</span><span class="n">clique</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span><span class="n">factors</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Factors for all the cliques or clusters not defined.&#39;</span><span class="p">)</span>

        <span class="n">cardinalities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">((</span><span class="n">x</span> <span class="k">for</span> <span class="n">clique</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">clique</span><span class="p">)))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">cardinalities</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Factors for all the variables not defined.&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">cardinality</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="n">factor</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">cardinalities</span><span class="p">[</span><span class="n">variable</span><span class="p">]</span> <span class="o">!=</span> <span class="n">cardinality</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s1">&#39;Cardinality of variable </span><span class="si">{var}</span><span class="s1"> not matching among factors&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="o">=</span><span class="n">variable</span><span class="p">))</span>

        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="ClusterGraph.copy"><a class="viewcode-back" href="../../../models.html#pgmpy.models.ClusterGraph.ClusterGraph.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a copy of ClusterGraph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        ClusterGraph: copy of ClusterGraph</span>

<span class="sd">        Examples</span>
<span class="sd">        -------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; G = ClusterGraph()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge((&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;))</span>
<span class="sd">        &gt;&gt;&gt; phi1 = DiscreteFactor([&#39;a&#39;, &#39;b&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; phi2 = DiscreteFactor([&#39;b&#39;, &#39;c&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(phi1, phi2)</span>
<span class="sd">        &gt;&gt;&gt; graph_copy = G.copy()</span>
<span class="sd">        &gt;&gt;&gt; graph_copy.factors</span>
<span class="sd">        [&lt;DiscreteFactor representing phi(a:2, b:2) at 0xb71b19cc&gt;,</span>
<span class="sd">         &lt;DiscreteFactor representing phi(b:2, c:2) at 0xb4eaf3ac&gt;]</span>
<span class="sd">        &gt;&gt;&gt; graph_copy.edges()</span>
<span class="sd">        [((&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;))]</span>
<span class="sd">        &gt;&gt;&gt; graph_copy.nodes()</span>
<span class="sd">        [(&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;)]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">copy</span> <span class="o">=</span> <span class="n">ClusterGraph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="n">factors_copy</span> <span class="o">=</span> <span class="p">[</span><span class="n">factor</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">]</span>
            <span class="n">copy</span><span class="o">.</span><span class="n">add_factors</span><span class="p">(</span><span class="o">*</span><span class="n">factors_copy</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">copy</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>